/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.services;

import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import test.services.intfc.ILoginService;

/**
 *
 * @author hjhi
 */
@Service("loginService")
public class LoginService implements ILoginService {

    ApplicationContext ac = new ClassPathXmlApplicationContext("//applicationContext.xml");
    DataSource dataSource = (DataSource) ac.getBean("dataSource");
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    @Override
    public int loginValidation(String uname, String pwd, String type) {
        String query = "select count(*) from users where username=? and password=? and type=?";

        Object[] parameters = new Object[]{uname, pwd, type};
        int c = jdbcTemplate.queryForInt(query, parameters);
        if (c == 1 && type.equals("general")) {
            return 1;
        } else if (c == 1 && type.equals("privileged")) {
            return 2;
        } else {
            return 0;
        }
    }
}
